package com.codewsq.springboot.service.impl;

import com.codewsq.springboot.entity.User;
import com.codewsq.springboot.mapper.UserMapper;
import com.codewsq.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /*@CachePut(cacheNames = "user", key = "#user.id+'-'+#result.wechat")*/
    @CachePut(cacheNames = "user", key = "#result.id")
    @Override
    public User insert(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public int insertSelective(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    //@Cacheable(cacheNames = "user", key = "#id", unless = "#result == null")
    @Override
    public User selectByPrimaryKey(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @CachePut(cacheNames = "user", key = "#user.id")
    @Override
    public User updateByPrimaryKeySelective(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        return user;
    }

    @Override
    public int updateByPrimaryKey(User user) {
        int i = userMapper.updateByPrimaryKey(user);
        return i;
    }
}
